package com.ms.content.letin;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ms.common.utils.R;
import com.ms.content.dao.BargainingItemDao;
import com.ms.content.entity.BargainingItemEntity;
import com.ms.content.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MqQueer {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private BargainingItemDao bargainingItemDao;

    @RabbitListener(queues = "orderinfo")
    public void msorder(String msg, Message message, Channel channel) throws IOException {
        String[] split = msg.split(",");
        Integer fid = Integer.valueOf(split[0]);
        Integer id = Integer.valueOf(split[1]);
        String uid = split[2];
        Integer sid = Integer.valueOf(split[3]);
        //添加订单表
        orderInfoService.insert(fid,id,uid,sid);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


    @RabbitListener(queues = "kj.o.update")
    public void scdd(String msg,Message message,Channel channel) throws IOException {

        BargainingItemEntity bargainingItemEntity = JSONObject.parseObject(msg, new TypeReference<BargainingItemEntity>() {
        });

        bargainingItemDao.updateById(bargainingItemEntity);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = "kj.order")
    public void add(String msg,Message message,Channel channel) throws IOException {

        BargainingItemEntity bargainingItemEntity = JSONObject.parseObject(msg, new TypeReference<BargainingItemEntity>() {
        });
        System.out.println("异步添加 : "+bargainingItemEntity);
        bargainingItemDao.insertst(bargainingItemEntity);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
